package view.editors;

import globalOperations.MarkupMethods;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.part.EditorPart;

import singleton.Singleton;
import swtElements.Form;
import swtElements.MainSection;
import swtElements.MyBrowser;
import swtElements.MyComposite;
import swtElements.MyLabel;
import swtElements.MySection;
import swtElements.MyText;
import view.editorActions.SaveDocumentation;
import view.views.TabEntrys;

/**
 * The DocumnetEditor class is a EditorPart
 * 
 * @author kristijanmadunic
 * 
 */
public class DocumentEditor extends EditorPart implements ModifyListener {

	/** Is true, if something changed in the editor */
	protected boolean dirty = false;

	/** Contains the text in the second tab (preview) */
	private MyBrowser browserPreview;

	/** The editor in the form */
	private MyText textEdit;

	/** Contains the categorys (the different tab's) */
	private TabEntrys tabentrys;

	/** There are two tab's: editor as form and preview */
	private TabFolder tabfolder;

	/** Textfield for the personal name */
	private MyText textFirstName;

	/** Textfield for the family name */
	private MyText textLastName;

	/** It's the current index(category) of the document */
	private int selIndex;

	/** Textfield for the title of the documentation */
	private MyText textTitle;

	/** Textfield to add extra search keys for the documentation */
	private MyText textTags;

	private FormToolkit toolkitPreview;

	private Form formPreview;

	private TabItem tabPreview;

	private MySection sectionPreview;

	/**
	 * When you want to save the document, the method is called
	 */
	@Override
	public void doSave(IProgressMonitor monitor) {
		SaveDocumentation.saveDocumentation(textFirstName, textLastName,
				textEdit, textTitle, textTags, selIndex);
		if (Singleton.getInstance().getResponse().compareTo("No Connection") != 0) {
			setDirty(false);
		}
	}

	@Override
	public void doSaveAs() {
		doSave(null);
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
	}

	/**
	 * This method returns the boolean dirty. Dirty is true, if something
	 * changed in the form
	 */
	@Override
	public boolean isDirty() {
		return dirty;
	}

	/**
	 * This method sets the dirty value
	 * 
	 * @param value
	 *            is true or false (dirty)
	 */
	protected void setDirty(boolean value) {
		dirty = value;
		firePropertyChange(PROP_DIRTY);
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	// private void setItAgain() {
	// IEditorPart thisEditor = getSite().getPage().getActiveEditor();
	// getSite().getPage().closeEditor(thisEditor, false);
	// setEditorBack(thisEditor);
	// }
	//
	// /* Setzt die Aktive alte Seite zurück */
	// private void setEditorBack(IEditorPart editor) {
	// try {
	// getSite().getWorkbenchWindow().getActivePage().openEditor(
	// editor.getEditorInput(), editor.getEditorSite().getId());
	// } catch (PartInitException e) {
	// e.printStackTrace();
	// }
	// }

	/**
	 * It's the main method of the class that creates all swt elements in the
	 * editor
	 */
	@Override
	public void createPartControl(Composite parent) {

		// Tabs

		tabentrys = new TabEntrys();
		tabfolder = new TabFolder(parent, SWT.BOTTOM);

		TabItem tabEdit = new TabItem(tabfolder, SWT.LEFT_TO_RIGHT);
		tabEdit.setText("Editor");

		tabPreview = new TabItem(tabfolder, SWT.LEFT_TO_RIGHT);
		tabPreview.setText("Preview");

		// Basic elements

		FormToolkit toolkitEdit = new FormToolkit(tabfolder.getDisplay());
		Form formEdit = new Form(toolkitEdit, tabfolder);

		// Info

		MainSection sectionInfo = new MainSection(toolkitEdit, formEdit);
		if (Singleton.getInstance().getDocumentText() != null) {
			sectionInfo.setText(Singleton.getInstance().getDocumentText());
		} else {
			sectionInfo.setText("");
		}

		// Author

		MySection sectionAuthor = new MySection(toolkitEdit, formEdit,
				"Author", false, "", false);

		MyComposite ClientAuthor = new MyComposite(toolkitEdit, sectionAuthor,
				4, false);

		new MyLabel(ClientAuthor, "First Name: ", false);

		textFirstName = new MyText(ClientAuthor, 235, false);

		new MyLabel(ClientAuthor, "Last Name: ", false);

		textLastName = new MyText(ClientAuthor, 235, false);

		sectionAuthor.setClient(ClientAuthor);

		// Tags

		MySection sectionTags = new MySection(toolkitEdit, formEdit, "Tags",
				true, "Enter your tags & seperate them with ';'", false);

		MyComposite ClientTags = new MyComposite(toolkitEdit, sectionTags, 1,
				false);

		// Get's the actuall tab of the document
		for (int i = 0; i < tabentrys.getSize(); i++) {
			if (tabentrys
					.getEntrys(i)
					.getTabItemName()
					.compareTo(
							Singleton.getInstance().getActualTab()
									.getTabItemName()) == 0) {
				selIndex = i;
			}
		}

		textTags = new MyText(ClientTags, 625, false);
		textTags.setText(Singleton.getInstance().getTags());

		// Add the listener
		textTags.addModifyListener(this);

		sectionTags.setClient(ClientTags);

		// Editor

		MySection sectionDoku = new MySection(toolkitEdit, formEdit,
				"Documentation", false, "", false);

		MyComposite ClientDoku = new MyComposite(toolkitEdit, sectionDoku, 3,
				false);

		new MyLabel(ClientDoku, "Title: ", false);

		new MyLabel(ClientDoku, "*", true);

		GridData gridTitle = new GridData();
		gridTitle.widthHint = 568;

		textTitle = new MyText(ClientDoku, 568, false);
		textTitle.setText(Singleton.getInstance().getTitle());

		// Add the listener
		textTitle.addModifyListener(this);

		new MyLabel(ClientDoku, "Text: ", false);

		new MyLabel(ClientDoku, "*", true);

		textEdit = new MyText(ClientDoku, 300, true);

		textEdit.setText(Singleton.getInstance().getText());

		// Add the listener
		textEdit.addModifyListener(this);

		sectionDoku.setClient(ClientDoku);
		tabEdit.setControl(formEdit.getForm());

		// Preview

		toolkitPreview = new FormToolkit(tabfolder.getDisplay());

		formPreview = new Form(toolkitPreview, tabfolder);

		StringBuilder tmp = new StringBuilder();
		String s = textTitle.getText();
		tmp.append(s);
		for (int i = 0; i < 100; i++) {
			tmp.append("        ");
		}
		sectionPreview = new MySection(toolkitPreview, formPreview,
				tmp.toString(), false, "", true);

		MyComposite ClientPreview = new MyComposite(toolkitPreview,
				sectionPreview, 1, false);

		browserPreview = new MyBrowser(ClientPreview, Singleton.getInstance()
				.getText(), 0, true);

		sectionPreview.setClient(ClientPreview);

		tabPreview.setControl(formPreview.getForm());
	}

	@Override
	public void setFocus() {
		tabfolder.setFocus();
	}

	/**
	 * It's a listener for the text areas, so the active preview is correct
	 */
	public void modifyText(ModifyEvent e) {
		// Remove the listeners
		textEdit.removeModifyListener(this);
		textTitle.removeModifyListener(this);

		sectionPreview.setText(textTitle.getText());
		browserPreview.setText(MarkupMethods.parseMarkUpToHTML(textEdit
				.getText()));

		// Save, if everything is filled, so title and doc!
		if (textEdit.getText().length() > 0 && textTitle.getText().length() > 0) {
			setDirty(true);
		} else {
			setDirty(false);
		}

		// Add the listeners back
		textEdit.addModifyListener(this);
		textTitle.addModifyListener(this);
	}
}